package com.software.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.software.dao.StuMngDao;
import com.software.dao.impl.MessageDetailDaoImpl;
import com.software.model.AgentCourseReal;
import com.software.model.AgentStudentReal;
import com.software.model.DrmAgentAllocateReal;
import com.software.model.DrmAgentAmountLog;
import com.software.model.DrmCourse;
import com.software.model.DrmUser;
import com.software.model.PageListModel;
import com.software.model.PageModel;
import com.software.service.StuMngService;
import com.software.utils.Constants;
import com.software.utils.IdCreater;

@Transactional
@Service("stuMngService")
public class StuMngServiceImpl implements StuMngService{
	
	@Autowired 
	private StuMngDao stuMngDao; 
	
	@Autowired
	private MessageDetailDaoImpl mongo;
	
	private int beginRow;
	
	private int endRow;
	
	@Override	
	public PageListModel mainList(PageModel vo) {

		Long cnt = stuMngDao.qureyCoursedetailsCnt(vo);

		PageListModel val = new PageListModel();
		
		if (0 < cnt) {
			vo.setTotalNum(Integer.valueOf(cnt.toString()));
			
			beginRow = (vo.getPageNo() - 1) * vo.getPageSize();
			val.setPage(vo);

			endRow = vo.getPageNo() * (vo.getPageSize());

			System.out.println("beginRow = " + beginRow);

			System.out.println("endRow = " + endRow);
			vo.setStartLine(beginRow);
			vo.setTotalLine(vo.getPageSize());
			List<DrmAgentAllocateReal> list = stuMngDao.qureyCoursedetails(vo);
			
			Map<String,String> stuMap = new HashMap<String,String>();
			Map<String,String> courseMap = new HashMap<String,String>();
			Map<String,String> courseTypeMap = new HashMap<String,String>();
			for (int i = 0; i < list.size(); i++) {
				val.getShowData().add(list.get(i));
				stuMap.put(list.get(i).getStudentId().toString(), list.get(i).getStudentId().toString());
				courseTypeMap.put(list.get(i).getCourseId().toString(),list.get(i).getCourseId().toString());
			}
			val.setStuCnt(stuMap.size());
			val.setCoursTypeCnt(courseTypeMap.size());

		}

		return val;
	}
		
	/**
	 * 创建学员
	 */
   @Override
   public void creatStu(DrmUser user,Long agentId){
	   
	   Long userId = IdCreater.getId();
	   user.setUserId(userId);
	   user.setUserTp(Constants.USER_TP_0);
	   user.setUserSource(Constants.USER_SOURCE_2);
	   user.setUserUyn(Constants.YES);
	   // 代理商id
	   user.setUserSourceid(agentId);
	   // 保存学员信息
	   stuMngDao.insertUser(user);
	   
	   AgentStudentReal real = new AgentStudentReal();
	   // 代理商id
	   real.setAgentId(agentId);
	   real.setStudentId(userId);
	   real.setId(IdCreater.getId());
	   // 保存代理商 学员关系
	   stuMngDao.insertAgentUserReal(real);
   }
   
   /**
    * 查下代理商的学员
    */
   @Override
   public List<DrmUser> searchUserList(DrmUser user){
	   
	   return stuMngDao.qureyUserList(user);
   }
	   
   /**
    * 查询代理商的课程
    */
	@Override
	public List<DrmCourse> searchCourseList(DrmUser user){
		
		 return stuMngDao.qureyCourseList(user);
	}
	
	/**
    * 保存三者关系
    */
	@Override
	@SuppressWarnings("unchecked")
	public void save(HashMap<String,Object> map){
		// 代理商ID
		Long agentId =(Long) map.get("agentid");
		// 学员IDS
		List<String> stuids = (ArrayList<String>) map.get("userIds");
		// 课程IDS
		List<String> courids = (ArrayList<String>) map.get("courIds");
		
		Long total = new Long(0);
		
		//Constants.TEMP_USER_ID；
		
		AgentCourseReal real = new AgentCourseReal();
		real.setAgentId(agentId);
		// 代理商所属课程
		List<AgentCourseReal> courList = stuMngDao.qureyCourseObjt(real);
		// 代理商预付款
		Long am = stuMngDao.qureyAgentAmount(agentId);
		
		
		for(String courid : courids) {
			for (AgentCourseReal course : courList) {
				if(courid.equals(course.getCourseId().toString())) {
					// 课程费用累计
					total += course.getCoursePrice();
				}
			}
			
		}
		if(am >= total*stuids.size() && am > 0){
			
			for(String stuID : stuids) {
				
				for(String courid : courids) {

					for (AgentCourseReal course : courList) {
						if(courid.equals(course.getCourseId().toString())) {
							DrmAgentAllocateReal agentAllocateReal = new DrmAgentAllocateReal();
							agentAllocateReal.setAgentId(agentId);
							agentAllocateReal.setCourseId(new Long(courid));
							agentAllocateReal.setStudentId(new Long(stuID));
							agentAllocateReal.setId(IdCreater.getId());
							agentAllocateReal.setAllocatePrice(course.getCoursePrice());
							// 保存三者关系
							stuMngDao.insertAgentThridRealg(agentAllocateReal);
							
							DrmAgentAmountLog log = new DrmAgentAmountLog();
							log.setId(IdCreater.getId());
							log.setAgentId(agentId);
							log.setConsumeAmount(course.getCoursePrice());
							log.setConsumeSourceid(new Long(courid));
							log.setConsumeSourcenm(course.getCourseNm());
							log.setConsumeTp(course.getCourseTp());
							log.setResidueAmount(am - course.getCoursePrice());
							log.setBeforeAmount(am);
							
							am = am - course.getCoursePrice();
							// 保存LOG     
							stuMngDao.insertDrmAgentAmountLog(log);
						}
					}
					
				}
			}
			DrmAgentAmountLog userAmount = new DrmAgentAmountLog();
			userAmount.setConsumeAmount(am);
			userAmount.setAgentId(agentId);
			stuMngDao.updateAmout(userAmount);
		}
		
		
	}
   
}
